<template>
  <div>
    <div style="margin-bottom: 20px">
      <el-button type="primary" @click="fyCreation"><i class="el-icon-plus"></i>促销费用</el-button>
    </div>
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item label="分公司">
        <el-select v-model="formInline.branchId" v-if="isBranch" style="width: 180px" @change="getDeptList" @visible-change="visibleChange" filterable>
          <el-option
            v-for="item in branchList"
            :label="item.branch_Name"
            :value="item.branch_Id">
          </el-option>
        </el-select>
        <el-input v-if="!isBranch" v-model="branchName" disabled></el-input>
      </el-form-item>
      <!--<el-form-item label="所属部门">-->
        <!--<el-select v-model="formInline.departmentId" v-if="isBranch" style="width: 180px" filterable>-->
          <!--<el-option-->
            <!--v-for="item in deptList"-->
            <!--:value="item.org_id"-->
            <!--:label="item.org_name">-->
          <!--</el-option>-->
        <!--</el-select>-->
        <!--<el-input v-if="!isBranch" v-model="orgName" disabled></el-input>-->
      <!--</el-form-item>-->
      <!--<el-form-item label="分公司">-->
        <!--<el-input v-model="this.branchName" style="width: 220px" disabled></el-input>-->
      <!--</el-form-item>-->
      <!--<el-form-item label="所属部门">-->
        <!--<el-input v-model="this.orgName" style="width: 220px" disabled></el-input>-->
      <!--</el-form-item>-->
      <el-form-item label="费用状态">
        <el-select v-model="formInline.state" filterable clearable style="width:200px">
          <el-option label="草稿" value="1"></el-option>
          <el-option label="未开始" value="2"></el-option>
          <el-option label="进行中" value="3"></el-option>
          <el-option label="已结束" value="4"></el-option>
          <el-option label="已生成代垫" value="99"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="促销周期">
        <el-date-picker
          :editable="false"
          v-model="formInline.time"
          value-format="yyyy-MM-dd"
          type="daterange"
          @change="timeChange"
          placeholder="选择日期范围">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="费用名称">
        <el-input v-model="formInline.promotionName"></el-input>
      </el-form-item>
      <el-form-item label="费用ID">
        <el-input v-model="formInline.id"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="isLoading"
      :data="tableData"
      style="width: 100%"
      border>
      <el-table-column
        show-overflow-tooltip
        prop="branchName"
        label="分公司"
        align="center"
        width="140">
      </el-table-column>
      <!--<el-table-column-->
        <!--show-overflow-tooltip-->
        <!--prop="departmentName"-->
        <!--label="部门"-->
        <!--align="center"-->
        <!--width="140">-->
      <!--</el-table-column>-->
      <el-table-column
        prop="id"
        label="促销费用ID"
        align="center"
        width="100">
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="promotionName"
        label="促销费用名称"
        width="180"
        align="center">
      </el-table-column>
      <el-table-column
        prop="promotionTypeName"
        label="费用类型"
        align="center"
        show-overflow-tooltip
        width="150">
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="supplyFactory"
        label="供货商"
        align="center"
        width="100">
      </el-table-column>
      <el-table-column
        prop="payModeName"
        label="补偿方式"
        align="center"
        width="100">
      </el-table-column>
      <el-table-column
        prop="promotionBeginTime"
        label="促销开始时间"
        show-overflow-tooltip
        align="center"
        width="150">
      </el-table-column>
      <el-table-column
        prop="promotionEndTime"
        label="促销结束时间"
        show-overflow-tooltip
        align="center"
        width="150">
      </el-table-column>
      <el-table-column
        prop="payBeginTime"
        label="补偿开始时间"
        show-overflow-tooltip
        align="center"
        width="150">
      </el-table-column>
      <el-table-column
        prop="payEndTime"
        label="补偿结束时间"
        show-overflow-tooltip
        align="center"
        width="150">
      </el-table-column>
      <el-table-column
        prop="appliPerson"
        label="创建人"
        align="center"
        width="100">
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="remark"
        label="备注"
        align="center"
        width="100">
      </el-table-column>
      <el-table-column
        prop="createTime"
        label="创建时间"
        show-overflow-tooltip
        align="center"
        width="150">
      </el-table-column>
      <el-table-column
        prop="chargeState"
        label="费用状态"
        align="center"
        width="100">
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        fixed="right"
        min-width="180" >
        <template scope="scope">
          <el-button v-if="scope.row.chargeState !== '草稿'" @click="handle(scope.$index, scope.row, $event)" size="small">详情</el-button>
          <el-button v-if="scope.row.chargeState === '草稿'" @click="handle(scope.$index, scope.row, $event)" size="small">编辑</el-button>
          <el-button v-if="scope.row.chargeState === '草稿'" @click="handle(scope.$index, scope.row, $event)" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--分页-->
    <el-pagination
      @current-change="handleCurrentChange"
      :page-size="Number(pageData.pageSize)"
      layout="total, prev, pager, next"
      :total="Number(total)"
      style="padding-top:10px;text-align: right;">
    </el-pagination>
    <!--详情弹窗-->
    <el-dialog title="费用详情" :visible.sync="detailDialog" top="10%" class="detail-info">
      <div class="form-item" v-if="detailData.detail.promotionType === 1">
        <pre style="margin: -10px;line-height: 28px">
          分公司：   {{detailData.detail.branchName}}
          所属部门： {{detailData.detail.departmentName}}
          促销费用名称：{{detailData.detail.promotionName}}
          费用类型：{{detailData.detail.promotionTypeName}}
          供应商：{{detailData.detail.supplyFactory}}
          促销周期：  {{detailData.detail.promotionBeginTime}}至{{detailData.detail.promotionEndTime}}
        </pre>
      </div>
      <div class="form-item" v-else>
        <pre style="margin: -10px;line-height: 28px">
          分公司：   {{detailData.detail.branchName}}
          所属部门： {{detailData.detail.departmentName}}
          促销费用名称：{{detailData.detail.promotionName}}
          费用类型：{{detailData.detail.promotionTypeName}}
          供应商：{{detailData.detail.supplyFactory}}
          促销周期：  {{detailData.detail.promotionBeginTime}}至{{detailData.detail.promotionEndTime}}
          补偿方式：  {{detailData.detail.payModeName}}
          补偿周期：  {{detailData.detail.payBeginTime}}至{{detailData.detail.payEndTime}}
        </pre>
      </div>
      <div class="form-item">
        <h4>备注</h4>
        <el-form label-width="180px">
            <el-input  type="textarea" disabled placeholder="请设置活动说明" v-model="detailData.detail.remark" style="width: 500px"></el-input>
        </el-form>
      </div>
    </el-dialog>
  </div>
</template>
<script>
  import { getInfo, activity } from '../../../config/proxy.js'
  // import { mapState } from 'vuex'
  import mixins from '../../../components/mixins.vue'
  export default {
    mixins: [mixins],
    data () {
      return {
        deptList: [],
        visible: false,
        formInline: {
          branchId: '',
          departmentId: '',
          promotionBeginTime: '',
          promotionEndTime: '',
          id: '',
          promotionName: '',
          time: '',
          state: ''
        },
        tableData: [],
        isLoading: false,
        pageData: {
          pageSize: 10,
          page: 1
        },
        total: '',
//      详情弹窗
        detailDialog: false,
        detailData: {
          detail: '',
          tableData: []
        }
      }
    },
    methods: {
      getDeptList (val) {
        if (this.visible === true) {
          this.formInline.departmentId = ''
        }
        var _data = {}
        this.branchList.forEach(item => {
          if (val === item.branch_Id) {
            _data.orgId = item.org_id
          }
        })
        getInfo.department(_data, (res) => {
          if (res.message === 'ok') {
            if (val === 1) {
              this.deptList = []
              res.content.forEach(element => {
                if (element.org_name.indexOf('公司') === -1) {
                  this.deptList.push(element)
                }
              })
              this.matchDept()
            } else {
              this.deptList = res.content
              this.matchDept()
            }
          } else {
            this.$message({
              showClose: true,
              message: res.message,
              type: 'error'
            })
          }
        })
      },  // 获取部门
      matchDept () { // 匹配 当前登录人的部门与部门列表
        for (let index = 0; index < this.deptList.length; index++) {
          const element = this.deptList[index]
          if (Number(this.formInline.departmentId) === Number(element.org_id)) {
            return
          } else {
            if (index === this.deptList.length - 1) {
              this.formInline.departmentId = ''
            }
          }
        }
      },
      visibleChange (val) {
        this.visible = val
      },
      fyCreation () {
        this.openNewWindow('fy_add', '新增促销费用', 'fy_add')
      },
      timeChange (val) {
        if (val) {
          this.formInline.promotionBeginTime = val[0]
          this.formInline.promotionEndTime = val[1]
        } else {
          this.formInline.promotionBeginTime = ''
          this.formInline.promotionEndTime = ''
        }
      },
      onSubmit () {
        this.isLoading = true
        var _data = Object.assign(this.formInline, this.pageData)
        activity.cxfy.list(_data, (res) => {
          console.log(res, '促销费用列表')
          if (res.message === 'ok') {
            this.tableData = res.content.content
            this.total = res.content.count
            this.pageData.page = res.content.page
            this.isLoading = false
          } else {
            this.isLoading = false
            this.$message({
              showClose: true,
              message: res.message,
              type: 'error'
            })
          }
        })
      },                  // 提交
      handleCurrentChange (val) {
        this.pageData.page = val
        this.onSubmit()
      },    // 分页
      handle (index, row, event) {
        const handleName = event.target.innerText
        switch (handleName) {
          case '详情':
            this.detailDialog = true
            activity.cxfy.detail({id: row.id}, (res) => {
              if (res.message === 'ok') {
                this.detailData.detail = res.content
                this.detailDialog = true
              } else {
                this.$message({
                  showClose: true,
                  message: res.message,
                  type: 'error'
                })
              }
            })
            break
          case '删除':
            this.$confirm('您确定要删除此活动费用？', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
              activity.cxfy.delete({id: row.id}, (res) => {
                if (res.message === 'ok') {
                  this.onSubmit()
                  this.$message({
                    type: 'success',
                    message: '删除成功'
                  })
                } else {
                  this.$message({
                    type: 'error',
                    message: res.message
                  })
                }
              })
            }).catch(() => {
            })
            break
          case '编辑':
            this.openNewWindow('fy_edit', '编辑促销费用', 'fy_add', row)
            break
        }
      }    // 操作
    },
    computed: {
      orgName: function (state) {
        return this.$store.state.user.info.parOrgName
      },
      orgId: function (state) {
        return this.$store.state.user.info.or_org_id
      },
      branchName: function (state) {
        return this.$store.state.user.info.branchId === 1 ? '总公司' : this.$store.state.user.info.branchName
      },
      branchId: function (state) {
        return this.$store.state.user.info.branchId
      },
      update_state: function () {
        return this.$store.state.updateState.cxfyState
      }
    },
    watch: {
      update_state (curVal, oldVal) {  // 监听 更新状态 查询list
        this.onSubmit()
      },
      branchList (a, b) {
        this.getDeptList(this.formInline.branchId)
      }
    },
    created () {
      this.formInline.branchId = this.branchId
      this.onSubmit()
//      this.formInline.departmentId = this.orgId
//      pm2 start ./bin/www --watch
    }
  }
</script>
<style>
</style>
